//
//  SEDoctorDetailReserveServiceCell.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/13.
//

import UIKit

class SEDoctorDetailReserveServiceCell: UITableViewCell {
    
    var num: Int = 0

    var model = SERegistrationDoctorModel()
    weak var dateViewDelegate: SERegistrationDoctorDateViewDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .white
        setLayerCorners(rect: bounds, radius: 6, corners: [.bottomLeft, .bottomRight])
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBOutlet private var collectionViewHeightConstraint: NSLayoutConstraint!
    
    func updateWithViewModel(_ model: SERegistrationDoctorModel) {
        self.model = model

        num = model.isOpen ? model.shiftList.count : (model.shiftList.count > model.defalultLines ? model.defalultLines : model.shiftList.count)

        collectionView.reloadData()

        let height = CGFloat(46 * num)
        collectionView.contentSize = CGSize(width: collectionView.width, height: height)
        collectionViewHeightConstraint.constant = collectionView.contentSize.height
    }
    
    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.backgroundColor = .white
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(SEDoctorDetailReserveServiceItemCell.nibForCell(),
                                    forCellWithReuseIdentifier: SEDoctorDetailReserveServiceItemCell.reuseIdentifier())
        }
    }
    
}

// MARK: - UICollectionViewDataSource

extension SEDoctorDetailReserveServiceCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return num
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEDoctorDetailReserveServiceItemCell.reuseIdentifier(), for: indexPath) as! SEDoctorDetailReserveServiceItemCell
        cell.dateViewDelegate = self
        cell.updateData(model.shiftList[indexPath.item], indexPath: indexPath)
        return cell
    }
}

extension SEDoctorDetailReserveServiceCell: UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.width, height: 46)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
}

extension SEDoctorDetailReserveServiceCell: SERegistrationDoctorDateViewDelegate {
    func registrationDoctorDateViewBtnAction(model: SERegistrationDoctorShift) {
        dateViewDelegate?.registrationDoctorDateViewBtnAction(model: model)
    }
}
